package ait.mediabroadcast.mediaserver;

import java.util.logging.Logger;

import org.teleal.cling.model.action.ActionInvocation;
import org.teleal.cling.model.message.UpnpResponse;
import org.teleal.cling.model.meta.Service;
import org.teleal.cling.support.contentdirectory.callback.Browse;
import org.teleal.cling.support.model.BrowseFlag;
import org.teleal.cling.support.model.DIDLContent;
import org.teleal.cling.support.model.SortCriterion;
import org.teleal.cling.support.model.container.Container;

import ait.mediabroadcast.helper.MyConstants;
import android.os.Handler;
import android.os.Message;

public class AndroidContentBrowseActionCallback extends Browse {

	private static Logger log = Logger
			.getLogger(AndroidContentBrowseActionCallback.class.getName());
	
	private Service mService;
	private Container mContainer;
	private String mContainerId;
	private Handler mHandler;
	
	public AndroidContentBrowseActionCallback(Service service, String objectID,
			BrowseFlag flag, String filter, long firstResult, Long maxResults,
			SortCriterion[] orderBy) {
		super(service, objectID, flag, filter, firstResult, maxResults, orderBy);
		
	}

	public AndroidContentBrowseActionCallback(Service service,
			Container container, BrowseFlag flag, Handler handler) {
		super(service, container.getId(), flag);
		mService = service;
		mContainer = container;
		mHandler = handler;
	}

	@Override
	public void received(ActionInvocation arg0, DIDLContent didl) {
		log.info("Received browse action DIDL descriptor, creating tree nodes");
		Object [] contents = { (Object) didl.getContainers(), (Object) didl.getItems(), mService};
		Message containerItemMsg = mHandler.obtainMessage(MyConstants.GET_CONTENT_DIR_COMPLETED, (Object) contents);
		containerItemMsg.sendToTarget();
	}

	@Override
	public void updateStatus(Status status) {
		
	}

	@Override
	public void failure(ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
		System.err.println(operation + "\n defaultMsg:" + defaultMsg);
		
	}

	
}
